package com.example.user3.demo.utils;

import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Jackie on 2016/1/30.
 * 过滤用户输入只能为金额格式
 */
public class CashierInputFilter implements InputFilter {
    Pattern mPattern;
    //输入的最大金额
//  private static final int MAX_VALUE = Integer.MAX_VALUE;
    public double maxValue;
    //小数点后的位数
    private static final int POINTER_LENGTH = 2;
    private static final String POINTER = ".";
    private static final String ZERO = "0";
    private String descContent="";

    public OutOfRangeListener getCallback() {
        return callback;
    }

    public void setCallback(OutOfRangeListener callback) {
        this.callback = callback;
    }

    public OutOfRangeListener callback;
    public interface OutOfRangeListener{
         void callBackOutOfRange();
         void callBackInOfRange();
    }

    public boolean isOnlyCanSmall() {
        return isOnlyCanSmall;
    }

    public void setOnlyCanSmall(boolean onlyCanSmall) {
        isOnlyCanSmall = onlyCanSmall;
    }

    boolean isOnlyCanSmall; //小于
    public void initMax(String maxValue){
        if (TextUtils.isEmpty(maxValue)) {
            this.maxValue = Integer.MAX_VALUE;
        } else {
            try {
                this.maxValue = Double.valueOf(maxValue);
            } catch (Exception e) {
                this.maxValue = Integer.MAX_VALUE;
            }
        }
        mPattern = Pattern.compile("([0-9]|\\.)*");
    }

    public CashierInputFilter(String maxValue) {
        initMax(maxValue);
    }
    public CashierInputFilter(String maxValue, String descContent) {
        this.descContent =descContent;
        initMax(maxValue);
    }
    /**
     * @param source 新输入的字符串
     * @param start  新输入的字符串起始下标，一般为0
     * @param end    新输入的字符串终点下标，一般为source长度-1
     * @param dest   输入之前文本框内容
     * @param dstart 原内容起始坐标，一般为0
     * @param dend   原内容终点坐标，一般为dest长度-1
     * @return 输入内容
     */
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String sourceText = source.toString();
        String destText = dest.toString();

        //验证删除等按键
        if (TextUtils.isEmpty(sourceText)) {
            if(callback!=null){
                callback.callBackInOfRange();
            }
            return "";
        }
        Matcher matcher = mPattern.matcher(source);
        //已经输入小数点的情况下，只能输入数字
        if (destText.contains(POINTER)) {
            if (!matcher.matches()) {
                return "";
            } else {
                if (POINTER.equals(source)) { //只能输入一个小数点
                    return "";
                }
            }
            //验证小数点精度，保证小数点后只能输入两位
            int index = destText.indexOf(POINTER);
            int length = dend - index;
            if (length > POINTER_LENGTH) {
                return dest.subSequence(dstart, dend);
            }
        } else {
            //没有输入小数点的情况下，只能输入小数点和数字，但首位不能输入小数点和0
            if (!matcher.matches()) {
                return "";
            } else {
                if ((POINTER.equals(source.toString())) && TextUtils.isEmpty(destText)) {  //首位不能输入小数点
                    return "";
                } else if (!POINTER.equals(source.toString()) && ZERO.equals(destText)) { //如果首位输入0，接下来只能输入小数点
                    return "";
                }

            }
        }
        //验证输入金额的大小
        double sumText = Double.parseDouble(destText + sourceText);
        if (sumText > maxValue||(sumText==maxValue&&isOnlyCanSmall)) {
            Log.i("zzy","maxValue"+Thread.currentThread());

            if(!TextUtils.isEmpty(descContent)){
            }
            if(callback!=null){
                callback.callBackOutOfRange();
            }

            return dest.subSequence(dstart, dend);
        }
        if(callback!=null){
            callback.callBackInOfRange();
        }
        return dest.subSequence(dstart, dend) + sourceText;
    }
}
